package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 查询符合条件的用户总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM user " +
            "<where> " +
            "   <if test='name != null and name.trim() != \"\"'> AND name LIKE CONCAT('%', #{name}, '%') </if> " +
            "   <if test='account != null and account.trim() != \"\"'> AND account LIKE CONCAT('%', #{account}, '%') </if> " +
            "   <if test='identification != null and identification.trim() != \"\"'> AND identification LIKE CONCAT('%', #{identification}, '%') </if> " +
            "   <if test='phone != null and phone.trim() != \"\"'> AND phone LIKE CONCAT('%', #{phone}, '%') </if> " +
            "   <if test='email != null and email.trim() != \"\"'> AND email LIKE CONCAT('%', #{email}, '%') </if> " +
            "   <if test='sex != null'> AND sex = #{sex} </if> " +
            "   <if test='age != null'> AND age = #{age} </if> " +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if> " +
            "   <if test='userType != null'> AND user_type = #{userType} </if> " +
            "   <if test='freeze != null'> AND freeze = #{freeze} </if> " +
            "</where>" +
            "</script>")
    int countUsers(@Param("name") String name, @Param("account") String account,
                   @Param("identification") String identification, @Param("phone") String phone,
                   @Param("email") String email, @Param("sex") String sex,
                   @Param("age") Integer age, @Param("communityId") Long communityId,
                   @Param("userType") Integer userType, @Param("freeze") Integer freeze);

    /**
     * 分页查询用户
     */
    @Select("<script>" +
            "SELECT * FROM user " +
            "<where> " +
            "   <if test='name != null and name.trim() != \"\"'> AND name LIKE CONCAT('%', #{name}, '%') </if> " +
            "   <if test='account != null and account.trim() != \"\"'> AND account LIKE CONCAT('%', #{account}, '%') </if> " +
            "   <if test='identification != null and identification.trim() != \"\"'> AND identification LIKE CONCAT('%', #{identification}, '%') </if> " +
            "   <if test='phone != null and phone.trim() != \"\"'> AND phone LIKE CONCAT('%', #{phone}, '%') </if> " +
            "   <if test='email != null and email.trim() != \"\"'> AND email LIKE CONCAT('%', #{email}, '%') </if> " +
            "   <if test='sex != null'> AND sex = #{sex} </if> " +
            "   <if test='age != null'> AND age = #{age} </if> " +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if> " +
            "   <if test='userType != null'> AND user_type = #{userType} </if> " +
            "   <if test='freeze != null'> AND freeze = #{freeze} </if> " +
            "</where> " +
            "ORDER BY id ASC " +
            "LIMIT #{size} OFFSET #{offset} " +
            "</script>")
    List<User> getUsers(@Param("name") String name, @Param("account") String account,
                        @Param("identification") String identification, @Param("phone") String phone,
                        @Param("email") String email, @Param("sex") String sex,
                        @Param("age") Integer age, @Param("communityId") Long communityId,
                        @Param("userType") Integer userType, @Param("freeze") Integer freeze,
                        @Param("size") int size, @Param("offset") int offset);
}
